package qf.api.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import qf.api.RedisCacheApi;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisCacheApiImpl implements RedisCacheApi {

    @Autowired
    private StringRedisTemplate template;

    @Override
    public boolean set(String key, String value, long expire) {

        template.opsForValue().set(key,value);

        return this.expire(key,expire);
    }

    @Override
    public String get(String key) {
        return template.opsForValue().get(key);
    }

    @Override
    public boolean deleteKey(String key) {
        return template.delete(key);
    }

    @Override
    public boolean expire(String key, long expire) {
        return template.expire(key,expire, TimeUnit.MILLISECONDS);
    }

    @Override
    public Map<Object, Object> hGetAll(String key) {
        return template.opsForHash().entries(key);
    }

    @Override
    public Set<String> setMembers(String key) {
        return template.opsForSet().members(key);
    }

    @Override
    public Long hdecr(String key, String field, Long decrby) {
        return template.opsForHash().increment(key,field,decrby);
    }

    @Override
    public Set<String> keys(String partten) {
        return template.keys(partten);
    }

    @Override
    public void hupdate(String key, Map fieldValue) {
        template.opsForHash().putAll(key, fieldValue);;
    }
}
